//
//  APImycityTodayTotal.m
//  Bracelet
//
//  Created by jing zhao on 2/26/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "APImycityTodayTotal.h"
#import "APIConnection.h"

#import <AFNetworking/AFNetworking.h>

#import "FileReadAndWrite.h"
#define FILENAMECACHE_APIMYCITYTOTAL @"fileName_apimycityTotal"


@implementation APImycityTodayTotal
-(instancetype) initFromTodayData:(NSDictionary*)dicInput
{
    self = [super init];
    if (self) {
      
        NSDictionary *dic = [dicInput objectForKey:APIKEYMYCITYDATA];
        
        self.strCityNextChanllengeTarget = [dic objectForKey:APIKEYCITYNEXTCHALLENGETARGET];
        self.strCityRunRanking = [dic objectForKey:APIKEYCITYRUNRANKING];
        self.strCityRunTotLeMetre = [dic objectForKey:APIKEYCITYRUNTOTLEMETRE];
        self.strCityRunTotleMoney = [dic objectForKey:APIKEYCITYRUNTOTLEMONEY];
        self.strCityStage = [dic objectForKey:APIKEYCITYSTAGE];
        self.strCityTarget = [dic objectForKey:APIKEYCITYTARGET];
        self.strCityTotleWalkers = [dic objectForKey:APIKEYCITYTOTLEWALKERS];
        self.strOkproportion = [dic objectForKey:APIKEYOKPROPORTION];
        self.strHtml = [dicInput objectForKey:APIKEYHTML];
    }
    
    return self;
}


+(instancetype)apiTodayWithData:(NSDictionary*)dic
{
    return [[self alloc]initFromTodayData:dic];
}


+(void)getMyCityTodayDataSuccess:(void (^)(APImycityTodayTotal *mycity))successBlock
 failure:(void (^)( NSError *error))failureBlock

{
    NSString *strCityId = [[[NSUserDefaults standardUserDefaults]objectForKey:APIKEYUSERINFO]objectForKey:APIKEYCITYID];
    NSDictionary *data = @{@"flag":@"mycity",@"cityId":strCityId,@"ios_android_flag":@"ios"};
    
    [[APIConnection shareInstance]post:@"mycity/getTotalData_city" data:data netError:^(NSError *error) {
         failureBlock(error);
        
        //取缓存
        NSDictionary *dicCanch = [FileReadAndWrite getTheStorageDataWithFile:FILENAMECACHE_APIMYCITYTOTAL];
        if (dicCanch) {
            NSDictionary *dicSuccess = [dicCanch objectForKey:APIKEYData];
             successBlock([APImycityTodayTotal apiTodayWithData:dicSuccess]);
        }
    } apiFinish:^(id data) {
        
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [dictionaryTemp objectForKey:APIKEYData];
            successBlock([APImycityTodayTotal apiTodayWithData:dicSuccess]);
            
            //存入缓存
            [FileReadAndWrite storageDataWithFile:FILENAMECACHE_APIMYCITYTOTAL andData:dictionaryTemp];
        }
        else
        {
            failureBlock(nil);
        }
   }];

}

@end
